var app = getApp();

Page({
    data: {
        imgUrl: app.globalData.imgUrl,
        systemInfo: {},
        padBtm:"25",
        sexList: [['女', '男']],
        sexId: "",
        sexPickerShow: false,
        date: '',
        datePickerValue: ['', '', ''],
        datePickerIsShow: false,
        avatar: "",
        thirdAvatar:"",
        imgFlag:true,
        userName:"",
        nickName: "",
        email: "",
        mobile: "",
        fileUrl:"",
    },
    onLoad: function () {
        var that = this;
        wx.getSystemInfo({
            success:function(res){
              that.setData({
                systemInfo:res,
              })
              if(res.model == "iPhone X"){
                    that.setData({
                        padBtm:"50",
                    })
                }
            }
        })
        that.setData({
            navH: app.globalData.navHeight
        })
        that.getUserInfo();
    },
    navBack: function () {
        wx.navigateBack({
            delta: 1
        })
    },
    sexPicker: function () {
        this.setData({
            sexPickerShow: true
        })
    },
    sureCallBack_sex(e) {
        var that = this;
        that.setData({
            sexPickerShow: false,
            sexId: e.detail.choosedIndexArr[0],
        })
    },
    cancleCallBack_sex() {
        this.setData({
            sexPickerShow: false,
        })
    },
    showDatePicker: function (e) {
        // this.data.datePicker.show(this);
        this.setData({
            datePickerIsShow: true,
        });
    },
    datePickerOnSureClick: function (e) {
        this.setData({
            date: `${e.detail.value[0]}-${e.detail.value[1]}-${e.detail.value[2]}`,
            datePickerValue: e.detail.value,
            datePickerIsShow: false,
        });
    },
    datePickerOnCancelClick: function (event) {
        this.setData({
            datePickerIsShow: false,
        });
    },
    changeAvatar: function () {
        var that = this;
        wx.chooseImage({
            count: 1, // 最多可以选择的图片张数，默认9
            sizeType: ['compressed'], // original 原图，compressed 压缩图，默认二者都有
            sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
            success: function (res) {
                var avatar = res.tempFilePaths[0];
                that.setData({
                    imgFlag:false,
                    tempFile:avatar,
                    fileUrl:avatar
                })
                //上传图片
                wx.uploadFile({
                    url: app.globalData.apiHost + "/api/common/upload",
                    filePath: that.data.fileUrl,
                    name: 'file',
                    formData: {
                        token: wx.getStorageSync('token'),
                        file: that.data.fileUrl
                    },
                    header: {
                        'content-type': 'multipart/form-data',
                        'Cookie': 'token=' + wx.getStorageSync('token')
                    },
                    success(res) {
                        var resImg=JSON.parse(res.data);
                        that.setData({
                            avatar: resImg.data.url
                        })
                    }
                })
            },
            fail: function (res) {
            },
            complete: function () {
                // complete
            }
        })
    },
    getUserInfo: function () {
        var that = this;
        wx.request({
            url: app.globalData.apiHost + "/api/user/info",
            data: {
                token: wx.getStorageSync('token')
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded',
                'Cookie': 'token=' + wx.getStorageSync('token')
            },
            method: "POST",
            dataType: "json",
            success: function (res) {
                if (res.statusCode === 200) {
                    var userData = res.data.data;
                    that.setData({
                        userName:userData.username,
                        nickName: userData.nickname,
                        email: userData.email,
                        mobile: userData.mobile,
                        avatar: userData.avatar,
                        thirdAvatar:userData.third_avatar,
                        sexId: userData.gender,
                        date: userData.birthday
                    })
                }
            },
            
        });
    },
    nickNameInput:function(e){
        this.setData({
            nickName: e.detail.value
        })
    },
    mobileInput:function(e){
        this.setData({
            mobile: e.detail.value
        })
    },
    emailInput:function(e){
        this.setData({
            email: e.detail.value
        })
    },
    formSubmit: function (e) {
        var that=this;
        var telReg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
        var emailRge = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
        if (e.detail.value.mobile.length == 0 || e.detail.value.email.length == 0) {
            wx.showToast({
                title: '手机号码和邮箱不得为空',
                icon: 'none',
                duration: 1500
            })
        }else if(e.detail.value.mobile.length!=11){
            wx.showToast({
                title: '手机号码长度有误',
                icon: 'none',
                duration: 1500
            })
        }else if(!telReg.test(that.data.mobile)){
            wx.showToast({
                title: '手机号码格式不正确',
                icon: 'none',
                duration: 1500
            })
        }else if(!emailRge.test(that.data.email)){
            wx.showToast({
                title: '邮箱格式不正确',
                icon: 'none',
                duration: 1500
            })
        }else if(e.detail.value.nickName.length == 0){
            wx.showToast({
                title: '昵称不能为空',
                icon: 'none',
                duration: 1500
            })
        }else{
            if(e.detail.value.sex=='女'){
                var sexNum=0;
            }else{
                var sexNum=1;
            }
            //提交表单
            wx.request({
                url: app.globalData.apiHost + "/api/user/profile",
                data: {
                    token: wx.getStorageSync('token'),
                    userName:that.data.userName,
                    nickname:e.detail.value.nickName,
                    email:e.detail.value.email,
                    avatar:that.data.avatar,
                    mobile:e.detail.value.mobile,
                    gender:sexNum,
                    birthday:e.detail.value.date
                },
                header: {
                    'content-type': 'application/x-www-form-urlencoded',
                    'Cookie': 'token=' + wx.getStorageSync('token')
                },
                method: "POST",
                dataType: "json",
                success: function (res) {
                    if (res.statusCode === 200) {
                        wx.showToast({
                            title: '修改成功',
                            icon: 'success',
                            duration: 1500
                        });
                        setTimeout(
                            function(){
                                wx.reLaunch({
                                    url: '../index/my-index'
                                });
                        },2000) 
                    }
                },
                fail: function (e) {
                },
                complete: function (obj) {
                }
            });
        }
    },
})
